﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Infrastructure.ObjectModel.Screens;
using Infrastructure.ObjectModel;
using B10_Ex02_Elad_034695114_Meyron_026607788.ObjectModel;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using B10_Ex02_EladLebovitch_034695114.ObjectModel;

namespace B10_Ex02_EladLebovitch_034695114.Screens
{
    public class LevelTransitionScreen : GameScreen
    {
        private const int k_SecondsToStart = 3;

        private SpriteFont m_ScoreFont;
        private Vector2 m_MessagePosition;
        private int m_nNumOfSeconds;
        private int m_nCurrLevel;
        private InvadorBackground m_Background;
        private TimeSpan tmsNew = new TimeSpan();
        
        public LevelTransitionScreen(Game i_Game)
            : base(i_Game)
        {
            m_Background = new InvadorBackground(i_Game, @"Sprites\BG_Space01_1024x768");
            this.Add(m_Background);
        }

        protected override void LoadContent()
        {
            base.LoadContent();
            m_ScoreFont = ContentManager.Load<SpriteFont>(@"Fonts\ReadyFont");
        }

        public override void Initialize()
        {
            base.Initialize();
            m_nNumOfSeconds = k_SecondsToStart;
            m_nCurrLevel = (Game.Services.GetService(typeof(GameFlow)) as GameFlow).CurrentLevel;

            // Set the positions for the text
            m_MessagePosition = new Vector2(50, CenterOfViewPort.Y - 100);
        }

        public override void Draw(GameTime gameTime)
        {
            base.Draw(gameTime);

            SpriteBatch.Begin();
            SpriteBatch.DrawString(m_ScoreFont, 
                                    "Get ready for level " + m_nCurrLevel + " in:\n" +
                                    "                 " + m_nNumOfSeconds.ToString(), m_MessagePosition, Color.Red);
            SpriteBatch.End();
        }

        public override void Update(GameTime gameTime)
        {
            // Check how much time passed
            tmsNew += gameTime.ElapsedGameTime;

            // If the time has passed
            if (tmsNew.Seconds >= 1)
            {
                tmsNew = tmsNew.Subtract(tmsNew);
                m_nNumOfSeconds--;
            }

            // TODO: Debug code to take out
            if (InputManager.KeyPressed(Microsoft.Xna.Framework.Input.Keys.Enter))
            {
                this.ExitScreen();
            }
            else if (InputManager.KeyPressed(Microsoft.Xna.Framework.Input.Keys.Escape))
            {
                this.Game.Exit();
            }

            // Let's start the game
            if (m_nNumOfSeconds == 0)
            {
                this.ExitScreen();
            }

            base.Update(gameTime);
        }
    }
}